package com.scan.bus.service;

import android.text.TextUtils;


import com.scan.bus.util.LogUtil;
import com.scan.bus.util.SharedPrefUtil;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by zhang on 2017/4/15 16 : 41.
 */

public class HttpRequestHeader implements Interceptor {

    private final boolean isRemoveContentType;

    public HttpRequestHeader(boolean isRemoveContentType) {
        this.isRemoveContentType = isRemoveContentType;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        String timestamp = System.currentTimeMillis() / 1000 + "";
        Request request = chain.request();
        Request.Builder builder = request.newBuilder();
//                .removeHeader("User-Agent")
//                .addHeader("Client-Type", "Android")
//                .addHeader("BUGLY-APP-CHANNEL", SharedPrefUtil.getCacheSharedPrf("BUGLY-APP-CHANNEL","null"))
//                .addHeader("Unique-Id", SharedPrefUtil.getCacheSharedPrf("uniqueId",""))
//                .addHeader("User-Agent", SharedPrefUtil.getCacheSharedPrf("User-Agent", "LianDong Android 5.1.1/2.0.0"))
//                .addHeader("X-Device-ID", SharedPrefUtil.getCacheSharedPrf("X-Device-ID", ""))
//                .addHeader("Resolution", SharedPrefUtil.getCacheSharedPrf("resolution", "720x1280"))
//                .addHeader("Net-Type", SharedPrefUtil.getCacheSharedPrf("Net-Type", "OTHER"))//网络类型
//                .addHeader("X-Client-Ip", SharedPrefUtil.getCacheSharedPrf("DeviceIp", "192.168.1.1"))
//                .addHeader("Token", SharedPrefUtil.getSharedUserString("token", ""))
////                .addHeader("X-Geo", SharedPrefUtil.getSharedUserString("Geo", ""))
//                .addHeader("X-Client-Time", timestamp);
//        if (isRemoveContentType) {
//            builder.removeHeader("Content-Type");
//        }
//        if (!TextUtils.isEmpty(Constant.WX_TEMP_TOKEN)){
//            builder.addHeader("oauth",Constant.WX_TEMP_TOKEN);
//        }
        HttpUrl httpUrl = request.url();
        String path = httpUrl.url().getPath();
        //对文件地址加密
//        String sha1 = Common.httpEncrypt(path,timestamp);
        //添加新url
        HttpUrl newHttpUrl = httpUrl.newBuilder()
                .scheme(httpUrl.scheme())
                .host(httpUrl.host())
                .port(httpUrl.port())
                 .build();
        builder.method(request.method(), request.body()).url(newHttpUrl);
        Response proceed = chain.proceed(builder.build());
        return proceed;
    }
}
